﻿using colorgetter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTools.UI
{
    public partial class CutScreenFrm : Form
    {
        public CutScreenFrm()
        {
            InitializeComponent();
        }
        int status = 0;
        Point start = new Point();
        Point preStart = new Point();
        Point end = new Point();

        private void CutScreenFrm_Load(object sender, EventArgs e)
        {
            this.TransparencyKey = Color.Red;
            this.BackColor = Color.Red;
            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
            this.WindowState = FormWindowState.Maximized;    //最大化窗体 
            this.KeyPreview = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (status == 2 || status == 3)
            {
                e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(start.X, start.Y, end.X - start.X + 1, end.Y - start.Y - 1));
            }
        }

        private void CutScreenFrm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
            {
                this.Close();
            }else if(e.KeyChar == (char)Keys.Enter)
            {
                copyThenClose();
            }
        }

        private void CutScreenFrm_MouseDown(object sender, MouseEventArgs e)
        {
            status = 1;
            preStart = start;
            start = e.Location;
            Console.Write("CutScreenFrm_MouseDown");

        }

        private void CutScreenFrm_MouseUp(object sender, MouseEventArgs e)
        {
            status = 3;
            end = e.Location;

            if(start.X!=end.X && start.Y != end.Y)
            {
                select = new Rectangle(start.X, start.Y, end.X - start.X + 1, end.Y - start.Y - 1);
            }
            else if(select.Width>0 && select.Height>0)
            {                
                start = new Point(select.X, select.Y);
                end = new Point(select.Right, select.Bottom);
            }

            
            this.Refresh();
        }
        Rectangle select = new Rectangle();

        private void CutScreenFrm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(select.Width<=0 || select.Height <= 0)
            {
                return;
            }
            start = preStart;
            copyThenClose();


        }
        void copyThenClose()
        {
            Size s = Win32Api.GetRealSize(new Size(end.X - start.X + 1, end.Y - start.Y - 1));
            Point p = Win32Api.GetRealPoint(this.Left + start.X, this.Top + start.Y);

            Bitmap b = new Bitmap(s.Width - 2, s.Height - 2);
            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(p.X + 2, p.Y + 2, 0, 0, b.Size);
            g.Dispose();
            Clipboard.SetImage(b);
            this.Close();
        }
    }
}
